package me.fangx.zhihu.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import me.fangx.common.util.eventbus.EventCenter;
import me.fangx.common.widget.loading.ProgressView;
import me.fangx.haorefresh.HaoRecyclerView;
import me.fangx.haorefresh.LoadMoreListener;
import me.fangx.zhihu.R;
import me.fangx.zhihu.adapter.CookListAdapter;
import me.fangx.zhihu.adapter.CookPagerAdapter;
import me.fangx.zhihu.modle.bean.CookBean;
import me.fangx.zhihu.presenter.CookPresenter;
import me.fangx.zhihu.utils.BaseUtil;
import me.fangx.zhihu.view.CookListView;

/**
 * Created by fangxiao on 16/1/25.
 */
public class CookCategoryFragment extends BaseFragment implements CookListView {

    @Bind(R.id.cook_swiperefresh)
    SwipeRefreshLayout cook_swiperefresh;
    @Bind(R.id.cook_recycleview)
    HaoRecyclerView cook_recycleview;

    //分类标识
    private int type = 0;

    private CookPresenter cookPresenter;
    private CookListAdapter cookListAdapter;
    private ArrayList<CookBean> listData = new ArrayList<>();
    private int page = 1;

    private BroadcastReceiver collectReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            CookBean cookBean = (CookBean) intent.getSerializableExtra("cookbean");
            if(type==cookBean.getType()){
                int position = listData.indexOf(cookBean);
                listData.get(position).setIsCollect(cookBean.getIsCollect());
                cookListAdapter.notifyItemChanged(position);
            }
        }
    };

    @Override
    protected void initViewsAndEvents() {
        Bundle bundle = getArguments();
        type = bundle.getInt(CookPagerAdapter.TYPE, 0);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.blue.recipe.collect");
        getActivity().registerReceiver(collectReceiver, intentFilter);
        cookListAdapter = new CookListAdapter(mContext, listData);
        cook_recycleview.setAdapter(cookListAdapter);
        cook_swiperefresh.setColorSchemeResources(R.color.textBlueDark, R.color.textBlueDark, R.color.textBlueDark,
                R.color.textBlueDark);

        cook_swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                cookPresenter.loadList(type, page);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        cook_recycleview.setLayoutManager(layoutManager);

        //设置自定义加载中和到底了效果
        ProgressView progressView = new ProgressView(mContext);
        progressView.setIndicatorId(ProgressView.BallPulse);
        progressView.setIndicatorColor(0xff69b3e0);
        cook_recycleview.setFootLoadingView(progressView);

        TextView textView = new TextView(mContext);
        textView.setText(R.string.already_to_end);
        cook_recycleview.setFootEndView(textView);

        cook_recycleview.setLoadMoreListener(new LoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                cookPresenter.loadList(type, page);
            }
        });


        cookPresenter = new CookPresenter(mContext);
        cookPresenter.attachView(this);
        //初次加载
        page = 1;
        cookPresenter.loadList(type, page);
//        showLoading(getString(R.string.loading));

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.cook_category_layout;
    }


    @Override
    public void refresh(List<CookBean> data) {
        //注意此处
//        hideLoading();
        cook_recycleview.refreshComplete();
        cook_recycleview.loadMoreComplete();
        cook_swiperefresh.setRefreshing(false);
        listData.clear();
        listData.addAll(data);
        cookListAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadMore(List<CookBean> data) {
        if (BaseUtil.isEmpty(data)) {
            cook_recycleview.loadMoreEnd();
        } else {
            listData.addAll(data);
            cookListAdapter.notifyDataSetChanged();
            cook_recycleview.loadMoreComplete();
        }
    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected View getLoadingTargetView() {
        return cook_swiperefresh;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    public void onDestroy() {
        getActivity().unregisterReceiver(collectReceiver);
        super.onDestroy();
    }
}
